<?php
/**
 * Helper functions that are used all over the application
 * @author Erko Risthein <erko@risthein.ee>
 * @copyright Copyright (C) 2009 Erko Risthein
 * @link http://erko.risthein.ee/decode/
 * @license http://www.gnu.org/licenses/ GNU General Public License
 * @package decode_explorer
 */
class Helper {
	/**
	 * Alternates between two values
	 * @param mixed $one first value
	 * @param mixed $two second value
	 * @return mixed one of the two values
	 */
	public static function alternate($one, $two) {
		static $i;
		return ($i++ % 2) ? $one : $two;
	}
	
	/**
	 * Formats the URL
	 * @param string $path
	 * @return string formatted URL
	 */
	public static function format_url($path) {
		return str_replace(array('%5C', '%2F'), '/', rawurlencode($path)); //replaces forward and backward slashes
	}
	
	/**
	 * Trims names longer than $length characters
	 * @param string $name
	 * @param string $length maximum length
	 * @return string trimmed name
	 */
	public static function trim_name($name, $length = 50) {
		if (mb_strlen($name) > $length) {
			$name = mb_substr($name, 0, $length - 5) . '...';
		}
		return $name;
	}
	
	/**
	 * Removes the rootpath
	 * @param string $path
	 * @return string formatted path
	 */
	public static function remove_rootpath($path) {
		return str_replace(DecodeExplorer::$rootpath, '.', $path);
	}
	
	/**
	 * Adds the rootpath
	 * @param string $path
	 * @return string formatted path
	 */
	public static function add_rootpath($path) {
		return preg_replace('/./', DecodeExplorer::$rootpath, $path, 1);
	}
}
?>